<!doctype html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
        <script src="../dist/sdk.dev.js"></script>
        <script src="./header.js"></script>
    </head>
    <body>
        <div style="padding: 24px">
            <button onclick="mockLight()">模拟 客厅灯 上报</button>
            <button onclick="mockPowerSocket2()">模拟 插座2 上报</button>
        </div>

        <div style="padding: 24px">
            <button onclick="getLightSwitch()">客厅灯 获取开关状态</button>
            <button onclick="getPowerSocketSwitch()">插座组 获取开关状态</button>
            <button onclick="setPowerSocketSwitchIterator()">插座组 循环设置开关状态</button>
        </div>

        <div style="padding: 24px">
            <button onclick="runCountdown()">定时 countdown</button>
        </div>

        <script>
            /**
             * 开启日志
             */
            DeviceManager.loglevel = 'log'; // log | info | warn | error

            /**
             * 客厅灯
             */
            const 客厅灯 = new DeviceManager.Light({
                name: '客厅灯',
            });

            客厅灯.subscribe((res) => {
                console.log('客厅灯.subscribe :>> ', res);
            });

            /**
             * 插座组
             */
            const 插座组 = new DeviceManager.PowerSocket({
                name: '插座',
                condition: true,
                group: true,
            });

            插座组.subscribe((res) => {
                console.log('插座组.subscribe :>> ', res);
            });

            插座组.condition(
                {
                    key: 'switch',
                    value: true,
                    method: 'getSwitch',
                },
                (res) => {
                    console.log('插座组.condition :>> ', res);
                },
            );

            /**
             * 打印 SDK
             */
            console.log('客厅灯 :>> ', 客厅灯);
            console.log('插座组 :>> ', 插座组);

            // 模拟 客厅灯 上报
            function mockLight() {
                subscribe({
                    from: '-2010010001000000001',
                    method: 'onReceive',
                    params: {
                        rgbColor: { red: 255, green: 255, blue: 255 },
                        switch: true,
                        flashTimes: 20,
                        flashDuration: { lightOn: 2, lightOff: 1 },
                        brightness: 60,
                    },
                });
            }

            // 模拟 插座2 上报
            function mockPowerSocket2() {
                subscribe({
                    from: '-2010010002000000002',
                    method: 'onReceive',
                    params: {
                        switch: true,
                        powerOffMemorySwitch: true,
                        powerOnState: 'ON',
                        indicatorSwitch: true,
                        indicatorMode: '1',
                        currentPower: 1200,
                        currentElectricity: 10,
                        currentVoltage: 220,
                        electricityStatistics: 3.83,
                        voiceCommandInfo: '打开',
                        voiceControlSwitch: true,
                        infraredControlSwitch: true,
                        fullPowerOffSwitch: true,
                        overloadProtectionSwitch: true,
                        fullPowerOffStatus: true,
                        overloadProtectionStatus: true,
                    },
                });
            }

            // 客厅灯 获取开关状态
            function getLightSwitch() {
                客厅灯.getSwitch()
                    .then((res) => {
                        console.log('客厅灯.getSwitch res :>> ', res);
                    })
                    .catch((err) => {
                        console.log('客厅灯.getSwitch err :>> ', err);
                    });
            }

            // 插座组 获取开关状态
            function getPowerSocketSwitch() {
                插座组.getSwitch()
                    .then((res) => {
                        console.log('插座组.getSwitch res :>> ', res);
                    })
                    .catch((err) => {
                        console.log('插座组.getSwitch err :>> ', err);
                    });
            }

            // 插座组 循环设置开关状态
            function setPowerSocketSwitchIterator() {
                for (const 插座 of 插座组) {
                    插座.setSwitch(Math.random() < 0.5)
                        .then((res) => {
                            console.log('插座.setSwitch res :>> ', res);
                        })
                        .catch((err) => {
                            console.log('插座.setSwitch err :>> ', err);
                        });
                }
            }

            /**
             * 通用方法
             */
            function runCountdown() {
                客厅灯.countdown({
                    duration: 60,
                    method: 'setSwitch',
                    params: {
                        value: false,
                    },
                })
                    .then((res) => {
                        console.log('客厅灯.countdown res :>> ', res);
                    })
                    .catch((err) => {
                        console.log('客厅灯.countdown err :>> ', err);
                    });
            }
        </script>
    </body>
</html>
